@extends('layouts.admin_common') @section('content')
<!-- SSLの場合 -->
@if(app('env')=='production')
<link rel="stylesheet" href="{{ secure_asset('/css/sale.css') }}">
<!-- <script src="{{ secure_asset('/js/jquery-3.3.1.min.js') }}"></script> -->
<script src="{{ secure_asset('/js/sale.js') }}"></script>
@else
<!-- SSLではない場合 -->
<link rel="stylesheet" href="{{ asset('/css/sale.css') }}">
<!-- <script src="{{ asset('/js/jquery-3.3.1.min.js') }}"></script> -->
<script src="{{ asset('/js/sale.js') }}"></script>
@endif

<div class="breadcrumbs_container">
    {{Breadcrumbs::render('admin.sale_add') }}
</div>

<script src="{{ asset('laydate/laydate.js') }}"></script>
<script>
laydate.render({
	  elem: '#start_date'
	  ,lang: 'en'
	  ,type: 'datetime'
	});
laydate.render({
	  elem: '#end_date'
	  ,lang: 'en'
	  ,type: 'datetime'
	});
</script>

<script>
window.onload = function(){
        document.getElementById("goodsId").onchange = function(){
    	var goodsname = this.value.split(',')[1];
        document.getElementById("goodsNameId").value = goodsname;
    }
}
function tsuika(obj){

    var obt = document.getElementById("saleCondictionId").value;
    alert(obt);
    alert(document.getElementById("saleCondictionId").lenght);

}
function addRow(obj){

    var obt = document.getElementById("seruOne");
    var count=0;
    obt.onclick = function () {
    count = count + 1;

        var newTr = addTable.insertRow();
        var newTd0 = newTr.insertCell();
        var newTd1 = newTr.insertCell();
        newTd0.innerHTML='<tr>'+
    						' <td align="center">1:合計数量指定数量以上&nbsp;<input type="hidden" name="SaleCondiction[condiction_type_'+count+']" value="1"/></td>'+
                            ' <td align="center"><input id="num_'+count+'" placeholder="数量" type="text" name="SaleCondiction[condiction_num_'+count+']" class="sale_select" size="6"/>&nbsp;&nbsp;&nbsp;</td>'+
                            ' <td align="center"><input id="data_'+count+'" placeholder="割引額" type="text" name="SaleCondiction[discount_value_'+count+']" class="sale_select" size="6"/>％ *数字を入力してください</td>'+
                			' <td>'+
                            ' <button type="button" id="delete_goods" class="button_delete button_normal" onclick="remove(this)"value="削除">削除</button> '+
                            '</td>'+
                	 '</tr>';
 }
}
function addRowTwo(obj){

    var obt = document.getElementById("seruTwo");
    var count=0;
    obt.onclick = function () {
    count = count + 1;

        var newTr = addTableTwo.insertRow();
        var newTd0 = newTr.insertCell();
        var newTd1 = newTr.insertCell();
        newTd0.innerHTML='<tr>'+
    						' <td align="center">2:同一商品指定数量以上&nbsp;<input type="hidden" name="SaleCondictionTwo[condiction_type_'+count+']" value="2"/></td>'+
                            ' <td align="center"><input id="num1" placeholder="数量" type="text" name="SaleCondictionTwo[condiction_num_'+count+']" class="sale_select" size="6"/>&nbsp;&nbsp;&nbsp;</td>'+
                            ' <td align="center"><input id="data1" placeholder="割引額" type="text" name="SaleCondictionTwo[discount_value_'+count+']" class="sale_select" size="6"/>％ *数字を入力してください</td>'+
                			' <td>'+
                            ' <button type="button" id="delete_goods" class="button_delete button_normal" onclick="remove(this)"value="削除">削除</button> '+
                            '</td>'+
                	 '</tr>';
 }
}
function addRowThree(obj){

    var obt = document.getElementById("seruThree");
    var count=0;
    obt.onclick = function () {
    count = count + 1;

        var newTr = addTableThree.insertRow();
        var newTd0 = newTr.insertCell();
        var newTd1 = newTr.insertCell();
        newTd0.innerHTML='<tr>'+
    						' <td align="center">3:合計価格指定数量以上&nbsp;<input type="hidden" name="SaleCondictionThree[condiction_type_'+count+']" value="3"/></td>'+
                            ' <td align="center"><input id="num1" placeholder="数量" type="text" name="SaleCondictionThree[condiction_num_'+count+']" class="sale_select" size="6"/>&nbsp;&nbsp;&nbsp;</td>'+
                            ' <td align="center"><input id="data1" placeholder="割引額" type="text" name="SaleCondictionThree[discount_value_'+count+']" class="sale_select" size="6"/>％ *数字を入力してください</td>'+
                			' <td>'+
                            ' <button type="button" id="delete_goods" class="button_delete button_normal" onclick="remove(this)"value="削除">削除</button> '+
                            '</td>'+
                	 '</tr>';
 }
}
function addUpdateRow(obj){

    var obt = document.getElementById("addUpdateID");
    var count=0;
    obt.onclick = function () {
    count = count + 1;

        var newTr = updateRow.insertRow();
        var newTd0 = newTr.insertCell();
        var newTd1 = newTr.insertCell();
        newTd0.innerHTML='<tr>'+
       					    ' <td align="center">'+
                            '<select id="corpId" name="SaleCondictionUpdate[condiction_type_'+count+']" class="sale_select" style="width:220px;">'+
                			'<option value="1">1:合計数量指定数量以上</option>'+
                			'<option value="2">2:同一商品指定数量以上</option>'+
                			'<option value="3">3:合計価格指定数量以上</option>'+
                		    '</select>'+
                		    '</td>'+
                            ' <td align="center">&nbsp;&nbsp;<input id="num1" placeholder="数量" type="text" name="SaleCondictionUpdate[condiction_num_'+count+']" value="" class="sale_select" size="6"/>&nbsp;&nbsp;&nbsp;&nbsp;</td>'+
                            ' <td align="center"><input id="data1" placeholder="割引額" type="text" name="SaleCondictionUpdate[discount_value_'+count+']" value="" class="sale_select" size="6"/>％</td>'+
	                		' <td>'+
                            ' <button type="button" id="delete_goods" class="button_delete button_normal" onclick="remove(this)"value="削除">削除</button> '+
                            '</td>'+
                	    '</tr>';
 }
}
function remove(temp){
	$(temp).parent().parent().remove();
}
</script>
<script>
function saleCondictionDelete(condictionId){
	if (false === confirm('削除しますか？') )return;
     $.ajax({
         type: 'POST',
         url: "../deleteCondictionJson",
         data: {condictionId: condictionId},
         dataType: 'json',
         success: function(status){
         	var $info = "";
             if (status == 1) {
            	 $('#t'+condictionId).remove();
             	$info="削除成功！";
                 $('#updateDeleteCondiction').css("color","#43CD80");
             } else {
             	$info="削除失敗！";
                 $('#updateDeleteCondiction').css("color","red");
             }
             $("#updateDeleteCondiction").html($info);
         }
     });
}

</script>

<div class="system_tab_line"></div>

<div id="sale_master_container">
	<form class="pure-form pure-form-stacked" method="POST" id="saleForm" enctype="multipart/form-data">
		<fieldset>
		@if(empty($allInfoUpdate[0]))
		<table>
		<tr>
				<td><span class="sale_line_title">セール名称</span></td>
				<td><input class="sale_select" id="sale_add_names_input" name="SaleMaster[sale_name]">
				*</td>
				<input type="hidden" id="sale_id" name="sale_id" value="">
			</tr>

			<tr>
                <td><span class="sale_line_title">@lang('messages.saleExp')</span></td>
    			<td>
    				<table class="sale_table">
                        <tr>
    					    <td align="center">
    							<div class="row">
    								<div class="col-sm-6">
    									<input type='text' class=" sale_input" id='start_date' name="SaleMaster[start_date]"/>
    								</div>
    							</div>
    						</td>
    					    <td>*
                                &nbsp;&nbsp;～&nbsp;&nbsp;
    					    </td>
    					    <td align="center">
    					    <div class="row">
    								<div class="col-sm-6">
    									<input type='text' class=" sale_input" id='end_date' name="SaleMaster[end_date]"/>
    								</div>
    							</div>
    					    </td>

    					    <td>
                                <span  class="sale_line_title margin_left_text">*  @lang('messages.showInTopPage')</span><input id="id_display_flg" type="checkbox" name="SaleMaster[display_flg]" value="1"/>
    					    </td>
                            <td>
                                <span class="sale_line_title margin_left_text">@lang('messages.image')</span>
    					    </td>
    					    <td id="sale_image_td">
    					    	 <div id="sale_image_container"></div>
                                    <div>
    					    	    <input id="pic" style="display:none;" type="file" name="photo" accept = "image/*" onchange = "selectFile()"/>

    					    	 </div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    					    	 <input id="select_image" class="button_add_sale pure-button pure-button-primary"
    					    	 type="button" value="{{trans('messages.reference')}}" onclick="clickSelectFile()">
    					    	 *
    					    </td>
                        </tr>
					</table>
				</td>
			</tr>
			<tr>
				<td align="left">
					@lang('messages.sale_goods')
					&nbsp;&nbsp;
				</td>
				<td>
                    <select name="" class="sale_select" style="width:240px;">
									@foreach($allInfo[0] as $categoryInfo)
										<option value="{{$categoryInfo->category_id}}">{{$categoryInfo->category_name}}</option>
									@endforeach
                    </select>*

					&nbsp;&nbsp;@lang('messages.good_code')&nbsp;&nbsp;
					<select name="SaleGoodsMaster[goods_id]" id="goodsId" class="sale_select" style="width:180px;">
									<option value="">選択してください</option>
									@foreach($allInfo[1] as $goodsInfo)
										<option value="{{$goodsInfo->goods_id}},{{$goodsInfo->goods_name}}">{{$goodsInfo->goods_code}}</option>
									@endforeach
                    </select>*
                    &nbsp;&nbsp;@lang('messages.good_name')&nbsp;&nbsp;
                    <input type="text" name="" id="goodsNameId" class="sale_select" size="22" value=" " readonly="true" style="background:#FFFFFF"/>
				</td>

			</tr>
			<tr>
				<td align="left">
					@lang('messages.saleCondition')&nbsp;&nbsp;
				</td>

				<td>
				   <table>
			        <tr>
			          <td>
						 <table>
        			        <td align="center">1:合計数量指定数量以上&nbsp;&nbsp;<input type="hidden" name="SaleCondiction[condiction_type_0]" value="1"/></td>
        			        <td align="center"><input id="num1" placeholder="数量" type="text" name="SaleCondiction[condiction_num_0]" class="sale_select" size="6"/>&nbsp;&nbsp;&nbsp;&nbsp;</td>&nbsp;&nbsp;
        			        <td align="center"><input id="data1" placeholder="割引額" type="text" name="SaleCondiction[discount_value_0]" class="sale_select" size="6"/>％  *数字を入力してください</td>
        			        <td align="center">&nbsp;&nbsp;<button type="button" class=" pure-button pure-button-primary button_normal"  value="追加" onclick="addRow();" id="seruOne" >追加</button></td>
					     </table>
					  </td>
			        </tr>
			        <tr>
			          <td>
						 <table id="addTable">
					     </table>
					  </td>
			        </tr>
			        <tr>
					   <td>
						 <table>
        			        <td align="center">2:同一商品指定数量以上&nbsp;&nbsp;<input type="hidden" name="SaleCondictionTwo[condiction_type_0]" value="2"/></td>
        			        <td align="center"><input id="num2" placeholder="数量" type="text" name="SaleCondictionTwo[condiction_num_0]" class="sale_select" size="6"/>&nbsp;&nbsp;&nbsp;&nbsp;</td>&nbsp;&nbsp;
        			        <td align="center"><input id="data2" placeholder="割引額" type="text" name="SaleCondictionTwo[discount_value_0]" class="sale_select" size="6"/>％  *数字を入力してください</td>
        			         <td align="center">&nbsp;&nbsp;<button type="button" class=" pure-button pure-button-primary button_normal"  value="追加" onclick="addRowTwo();" id="seruTwo" >追加</button></td>
 						 </table>
					  </td>
			        </tr>
			        <tr>
			          <td>
						 <table id="addTableTwo">
					     </table>
					  </td>
			        </tr>
			        <tr>
					   <td>
						 <table>
    			        <td align="center">3:合計価格指定数量以上&nbsp;&nbsp;<input type="hidden" name="SaleCondictionThree[condiction_type_0]" value="3"/></td>
    			        <td align="center"><input id="num3" placeholder="数量"  type="text" name="SaleCondictionThree[condiction_num_0]" class="sale_select" size="6"/>&nbsp;&nbsp;&nbsp;&nbsp;</td>&nbsp;&nbsp;
    			        <td align="center"><input id="data3" placeholder="割引額" type="text" name="SaleCondictionThree[discount_value_0]" class="sale_select" size="6"/>％  *数字を入力してください</td>
    			          <td align="center">&nbsp;&nbsp;<button type="button" class=" pure-button pure-button-primary button_normal"  value="追加" onclick="addRowThree();" id="seruThree" >追加</button></td>
						 </table>
					  </td>
			        </tr>
			        <tr>
			          <td>
						 <table id="addTableThree">
					     </table>
					  </td>
			        </tr>
		          </table>
				</td>
            </tr>

			<tr>
				<td align="left">
					会社選択&nbsp;&nbsp;
				</td>
				<td>
					<select id="corpId" name="SaleCorpMaster[corp_id]" class="sale_select" style="width:240px;">
									@foreach($allInfo[2] as $companyInfo)
										<option value="{{$companyInfo->corp_id}}">{{$companyInfo->corp_name}}</option>
									@endforeach
                    </select>
				</td>
			</tr>
			<tr>
			    <td>&nbsp;</td>
				<td>&nbsp;</td>
			</tr>
			<tr>
			    <td></td>
    			<td>
    			<button id="button_save_sale" type="button" onclick="saveSale('create');"
    				class="pure-button pure-button-primary button_normal">@lang('messages.save')</button>&nbsp;&nbsp;&nbsp;&nbsp;
    			<button id="button_cancel" type="button" onclick="cancelAddSale();"
    				class="pure-button pure-button-primary button_normal">@lang('messages.cancel')</button>&nbsp;&nbsp;&nbsp;&nbsp;
    			<button id="button_cancel" type="button" onclick="onSubmitGo();"
    				class="pure-button pure-button-primary button_normal">戻る</button>
				</td>
			</tr>
			</table>
		@else
		<table>
		<tr>
				<td><span class="sale_line_title">セール名称</span></td>
				<td><input class="sale_select" id="sale_add_names_input" name="SaleMaster[sale_name]" value="{{$allInfoUpdate[0][0]->sale_name}}"></td>
				<input type="hidden" id="sale_id" name="SaleMaster[sale_id]" value="{{$allInfoUpdate[0][0]->sale_id}}">
			</tr>

			<tr>
                <td><span class="sale_line_title">@lang('messages.saleExp')</span></td>
    			<td>
    				<table class="sale_table">
                        <tr>
    					    <td align="center">
    							<div class="row">
    								<div class="col-sm-6">
    									<input type='text' class="sale_input" id='start_date' name="SaleMaster[start_date]" value="{{$allInfoUpdate[0][0]->start_date}}"/>
    								</div>
    							</div>
    						</td>
    					    <td>
                                &nbsp;&nbsp;～&nbsp;&nbsp;
    					    </td>
    					    <td align="center">
    					    <div class="row">
    								<div class="col-sm-6">
    									<input type='text' class="sale_input" id='end_date' name="SaleMaster[end_date]" value="{{$allInfoUpdate[0][0]->end_date}}"/>
    								</div>
    							</div>
    					    </td>
    					    <td>
                                <span  class="sale_line_title margin_left_text">@lang('messages.showInTopPage')</span>
                                <input id="id_display_flg" type="hidden" name="SaleMaster[display_flg]" value="{{$allInfoUpdate[0][0]->display_flg}}"/>
                                <input type="checkbox" id="id_display_flg" value=""
                                <?php if ('1' == $allInfoUpdate[0][0]->display_flg) {echo 'checked';}?> />
    					    </td>
                            <td>
                                <span class="sale_line_title margin_left_text">@lang('messages.image')</span>
    					    </td>
    					    <td>&nbsp;&nbsp;&nbsp;&nbsp;
								<img id="portrait" src="{{$allInfoUpdate[0][0]->filesrc}}" style="width: 190px; height: 190px;" />
    					    	 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    					    	<label for="sample1" class="btn btn-primary button_normal" style="width:40px;margin-left:60px;margin-top:2px;">{{trans('messages.reference')}}
									<input type="file" name="photo" id="sample1"
									style="display: none;" class="btn btn-primary button_normal"
									onChange="showPreview(this)" />
							    </label>
							    <input type="hidden" id="filesrc" name="filesrc" value="{{$allInfoUpdate[0][0]->filesrc}}">
    					    </td>
                        </tr>
					</table>
				</td>
			</tr>
			<tr>
				<td align="left">
					@lang('messages.sale_goods')&nbsp;&nbsp;
				</td>
				<td>
                    <select name="" class="sale_select" style="width:240px;">
									@foreach($allInfoUpdate[1] as $categoryInfo)
										<option value="{{$categoryInfo->category_id}}">{{$categoryInfo->category_name}}</option>
									@endforeach

                    </select>

					&nbsp;&nbsp;@lang('messages.good_code')&nbsp;&nbsp;
					<select name="SaleGoodsMaster[goods_id]" id="goodsId" class="sale_select" style="width:150px;">
									@foreach($allInfoUpdate[2] as $goodsInfo)
										<option value="{{$goodsInfo->goods_id}},{{$goodsInfo->goods_name}}"
										<?php if ($goodsInfo->goods_id == $allInfoUpdate[0][0]->goods_id) {echo 'selected';}?>>
										{{$goodsInfo->goods_code}}
										</option>
									@endforeach
                    </select>
                    &nbsp;&nbsp;@lang('messages.good_name')&nbsp;&nbsp;
                    <input type="text" name="" id="goodsNameId" class="sale_select" size="22" value="{{$allInfoUpdate[0][0]->goods_name}}" readonly="true" style="background:#FFFFFF"/>
				</td>

			</tr>
			<tr>
				<td align="left">
					@lang('messages.saleCondition')&nbsp;&nbsp;
				</td>

				<td>
				   <table>
			        <tr>
    			        <td align="center">条件種類&nbsp;&nbsp;</td>
    			        <td align="center">数量&nbsp;&nbsp;</td>
    			        <td align="center">割引額&nbsp;&nbsp;</td>
    			         <td align="center">&nbsp;&nbsp;</td>
			        </tr>
			        <?php $i=0;?>
        			<?php foreach ($allInfoUpdate[4] as $saleCondiction):?>
        				<tr id="t{{$saleCondiction->condiction_id}}">
        				<td align="center">
        				 <?php if ('1' == $saleCondiction->condiction_type) {echo '1:合計数量指定数量以上';}?>
        				 <?php if ('2' == $saleCondiction->condiction_type) {echo '2:同一商品指定数量以上';}?>
        				 <?php if ('3' == $saleCondiction->condiction_type) {echo '3:合計価格指定数量以上';}?>
        				 <input type="hidden" name="SaleCondiction[condiction_id_{{$i}}]" value="{{$saleCondiction->condiction_id}}"/>
        				 <input type="hidden" name="SaleCondiction[condiction_type_{{$i}}]" value="{{$saleCondiction->condiction_type}}"/>
        				 </td>
    			        <td align="center">&nbsp;&nbsp;<input id="num1" type="text" name="SaleCondiction[condiction_num_{{$i}}]" value="{{$saleCondiction->condiction_num}}" class="sale_select" size="6"/>&nbsp;&nbsp;&nbsp;&nbsp;</td>&nbsp;&nbsp;</td>
    			        <td align="center"><input id="data1" type="text" name="SaleCondiction[discount_value_{{$i}}]"value="{{$saleCondiction->discount_value}}" class="sale_select" size="6"/>％</td>
						 <td align="center">&nbsp;&nbsp;<button type="button" id="delete_goods" class="button_delete button_normal" onclick="saleCondictionDelete('{{$saleCondiction->condiction_id}}')" value="削除">削除</button></td>

						</tr>
        			<?php $i++;?>
        			<?php endforeach;?>
		          </table>
				</td>
            </tr>
            <tr>
				<td align="left">

				</td>

				<td>
				   <table id="updateRow">
			        <tr>
    			        <td align="center">&nbsp;&nbsp;</td>
    			        <td align="center">&nbsp;&nbsp;</td>
    			        <td align="center">&nbsp;&nbsp;</td>
    			         <td align="center"><span id="updateDeleteCondiction"></span></td>
			        </tr>
		          </table>
				</td>
            </tr>
			<tr>
				<td align="left">
					会社選択&nbsp;&nbsp;
				</td>
				<td>
					<select id="corpId" name="SaleCorpMaster[corp_id]" class="sale_select" style="width:240px;">
									@foreach($allInfoUpdate[3] as $companyInfo)
										<option value="{{$companyInfo->corp_id}}"
										<?php if ($companyInfo->corp_id == $allInfoUpdate[0][0]->corp_id) {echo 'selected';}?>>
										{{$companyInfo->corp_name}}
										</option>
									@endforeach
                    </select>
				</td>
			</tr>
			<tr>
			    <td>&nbsp;</td>
				<td>&nbsp;</td>
			</tr>
			<tr>
			    <td></td>
    			<td>
    			<button id="button_save_sale" type="button" onclick="saveUpdateSale();"
				class="pure-button pure-button-primary button_normal">@lang('messages.save')</button>&nbsp;&nbsp;&nbsp;&nbsp;
				<button id="button_cancel" type="button" onclick="cancelUpdateSale();"
				class="pure-button pure-button-primary button_normal">@lang('messages.cancel')</button>&nbsp;&nbsp;&nbsp;&nbsp;
				<button id="button_cancel" type="button" onclick="onSubmitGo();"
				class="pure-button pure-button-primary button_normal">戻る</button>&nbsp;&nbsp;&nbsp;&nbsp;
				<button type="button" class=" pure-button pure-button-primary button_normal"  value="追加" onclick="addUpdateRow();" id="addUpdateID" >追加</button>
				</td>
			</tr>

		</table>
		@endif
		</fieldset>
		<input type="hidden" name="_token" id="csrf-token" value="{{ Session::token() }}" />
	</form>
</div>
@endsection
